﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace BusinessLogic.Command
{
    public class MaintainEmployerCommand : BaseCommand<object>
    {
        private List<APP_EMPLOYER> _list;
        CubeOfDB2Entities ctx = new CubeOfDB2Entities();
    
        public MaintainEmployerCommand(List<APP_EMPLOYER> empList)
        {
            _list = empList;
        }

        public override void Command()
        {
            foreach (APP_EMPLOYER row in _list)
            {
                if (row.act == ActionEnum.Add || (row.ID == 0 && row.act != ActionEnum.Delete) || (row.ID == null && row.act != ActionEnum.Delete))
                {
                    ctx.APP_EMPLOYER.AddObject(row);

                    if (row.APP_PROJECT.Count > 0)
                        SetBackReference(row.APP_PROJECT.ToList(), row);
                }
                else if(row.act == ActionEnum.Edit)
                {
                    APP_EMPLOYER e = ctx.APP_EMPLOYER.Where(o => o.ID == row.ID).FirstOrDefault();
                    if (e != null)
                    {
                        e.PERSONAL_ID = row.PERSONAL_ID;
                        e.NAME = row.NAME;
                        e.ADDRESS = row.ADDRESS;
                        e.BENEFITS = row.BENEFITS;
                        e.BUS_TYPE = row.BUS_TYPE;
                        e.LAST_POSITION = row.LAST_POSITION;
                        e.LATEST_SALARY = row.LATEST_SALARY;
                        e.LEAV_REASON = row.LEAV_REASON;
                        e.SINCE = row.SINCE;
                        e.SRT_SALARY = row.SRT_SALARY;
                        e.SUPERVISOR_NAME = row.SUPERVISOR_NAME;
                        e.TOT_MTH = row.TOT_MTH;
                        e.TOT_YEAR = row.TOT_YEAR;
                        e.UNTIL = row.UNTIL;

                        if (row.APP_PROJECT.Count > 0)
                            SetBackReference(row.APP_PROJECT.ToList(), e);
                    }
                }
                else if(row.act == ActionEnum.Delete)
                {
                    APP_EMPLOYER e = ctx.APP_EMPLOYER.Where(o => o.ID == row.ID).FirstOrDefault();
                    if (e != null)
                    {
                        if (row.APP_PROJECT.Count > 0)
                            SetBackReference(row.APP_PROJECT.ToList(), e, true);

                        ctx.DeleteObject(e);
                    }
                }

                

                ctx.SaveChanges();
            }
        }

        private void SetBackReference(List<APP_PROJECT> list, APP_EMPLOYER row, bool empDelete=false)
        {
            foreach (APP_PROJECT proj in list)
            {
                
                if (proj.act == ActionEnum.Add || (proj.ID == 0 && proj.act != ActionEnum.Delete) || (proj.ID == null && proj.act != ActionEnum.Delete))
                {
                    proj.APP_EMPLOYER = row;
                    ctx.APP_PROJECT.AddObject(proj);
                }
                else if (proj.act == ActionEnum.Edit)
                {
                    APP_PROJECT p = ctx.APP_PROJECT.Where(o => o.ID == proj.ID).FirstOrDefault();
                    if (p != null)
                    {
                        p.CLIENT_NAME = proj.CLIENT_NAME;
                        p.DB = proj.DB;
                        p.FRAMEWORK = proj.FRAMEWORK;
                        p.LANG = proj.LANG;
                        p.OS = proj.OS;
                        p.PERIOD_FROM = proj.PERIOD_FROM;
                        p.PERIOD_TO = proj.PERIOD_TO;
                        p.PROJ_DESC = proj.PROJ_DESC;
                        p.PROJ_NAME = proj.PROJ_NAME;
                        p.ROLE = proj.ROLE;
                        p.TEAM = proj.TEAM;
                        p.TOOLS = proj.TOOLS;
                    }
                }
                else if (proj.act == ActionEnum.Delete || empDelete)
                {
                    APP_PROJECT p = ctx.APP_PROJECT.Where(o => o.ID == proj.ID).FirstOrDefault();
                    if (p != null)
                    {
                        ctx.APP_PROJECT.DeleteObject(p);
                    }
                }
            }
        }
    }
}
